package com.yuanye.recruitCrawler.manual;

import java.util.Date;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.yuanye.recruitCrawler.constant.Job51Constant;
import com.yuanye.recruitCrawler.entity.Position;
import com.yuanye.recruitCrawler.utils.ScannerUtil;
import com.yuanye.recruitCrawler.utils.SeleniumUtil;
/**
 * @author yuanye 2021年7月17日 下午4:27:32
 */
public class Job51{
	private static Logger log = LoggerFactory.getLogger(Job51.class);

	public static void main(String[] args) {
		String url="https://we.51job.com/pc/search?keyword=Java&searchType=2&sortType=0&metro=";
		WebDriver driver = SeleniumUtil.getChromeDriver();
		driver.get(url);
		while(ScannerUtil.scannerReady()) {
			extractPositions(driver);
		}
		if(driver!=null) {
			driver.quit();
		}
	}
	
	public static void extractPositions(WebDriver driver) {
		log.info("extracting URL:{}", driver.getCurrentUrl());
		List<WebElement> content = driver.findElements(By.xpath(Job51Constant.positionList));
		System.out.println("当前页职位数：" + content.size());
		for (int i = 0; i < content.size(); i++) {
			WebElement node = content.get(i);
			// System.out.println(node.getText());
			try {
				String positionName = node.findElement(By.xpath(Job51Constant.positionName)).getText();
				String positionUrl = node.findElement(By.xpath(Job51Constant.positionUrl)).getAttribute("href");
				if (positionUrl != null && positionUrl.indexOf("jobs.51job.com") > 0) {
					positionUrl = positionUrl.substring(0, positionUrl.indexOf(".html") + 5);
				}
				String salary = node.findElement(By.xpath(Job51Constant.salary)).getText();
				String experience = node.findElement(By.xpath(Job51Constant.experience)).getText();
				// String education=node.findElement(By.xpath(Job51Constant.education)).getText();
				// String attraction=node.findElement(By.xpath(Job51Constant.attraction)).getText();
				String companyName = node.findElement(By.xpath(Job51Constant.companyName)).getText();
				String companyUrl = node.findElement(By.xpath(Job51Constant.companyUrl)).getAttribute("href");
				String industry = node.findElement(By.xpath(Job51Constant.industry)).getText();
				String address = node.findElement(By.xpath(Job51Constant.address)).getText();
				String publishTime = node.findElement(By.xpath(Job51Constant.publishTime)).getText();
				String source = "51job";
				Position position = new Position();
				position.setPositionName(positionName);
				position.setPositionUrl(positionUrl);
				position.setSalary(salary);
				position.setCompanyName(companyName);
				position.setCompanyUrl(companyUrl);
				position.setAddress(address);
				position.setPublishTime(publishTime);
				position.setSource(source);
				position.setCreateTime(new Date());
				position.setExperience(experience);
				// position.setKeyWords(keyWords);
				// position.setAttraction(attraction);
				position.setIndustry(industry);
				// position.setCompanyLogo(companyLogo);
				System.out.println(JSON.toJSONString(position));
				//saveJD...
			} catch (Exception e) {
				log.error(e.getMessage(), e);
			}
		}
		content = null;
	}
}